<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link href="${resources}/plugins/fontAwesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <link href="${resources}/css/common.css" rel="stylesheet" type="text/css">
    <link href="${resources}/css/cart.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="container">

    <!--头部-->
    <div class="commonHeader">购物车
        <a href="javascript:void (0)" class="fa fa-chevron-left back"></a>
    </div>
    
<#list cartGoodsList as goodsList > 
    <div class="shops">
        <!--店铺-->
        <div class="shop">
            <div class="shop-title">${goodsList.merchant.name}<span></span></div>

            <!--商品列表-->
      <#list goodsList.list as goods>
            <div class="goods">
               <input type="checkbox" class="checkbox checkboxSel checkboxSel_${goods.goods.id}" name="checkboxname" value="${goods.goods.id}"  <#if goods.status == 2> checked="true" </#if>  >
               
               <input type="hidden"  id="goodsId" value="${goods.id}">
               <input type="hidden"  id="merchantId" value="${goodsList.merchant.id}">
               <input type="hidden"  id="score" value="${goods.goods.price*merchantRate.score}"> 
               <div class="goods-pic">
                    <img src="${goods.goods.picurl}" alt="">
                </div>
                <h3 class="goods-name">${goods.goods.name}</h3>
                <div class="goods-price">
                    <span>积分 ${goods.goods.price*merchantRate.score}</span>
                    <span class="goodsNum" id="goodsNum_${goods.goods.id}">x${goods.amount}</span>
                </div>
                <div class="num-module" id="num-module">
                    <span class="minus">-</span>
                    <input type="text" id="amount" class="amount_${goods.id}"  value="${goods.amount}">
                    <input type="hidden" class="goodsId" value="${goods.goods.id}">
                    <input type="hidden"  id="score" value="${goods.goods.price*merchantRate.score}"> 
                    <input type="hidden"  id="merchantId_num" value="${goodsList.merchant.id}"> 
                    <span class="plus enable">+</span>
                </div>
            </div>
       </#list>
             
            <div class="subtotal">
                <span>小计积分：</span> 
                <span class="subprice" id="xiaojiScore_${goodsList.merchant.id}">${goodsList.totalPrice*merchantRate.score}</span> 
               
            </div>
   </div>

  </#list>       
        

    <div class="total">
        <input type="checkbox" class="checkbox selectAll " id="selectAll"><label for="selectAll">全选</label>

        
        <p class="total-price" >合计积分：<span id="total-price">${allPrice*merchantRate.score}</span> </p> 

        <a class="submit-order" href="javascript:void(0)">提交订单</a>
    </div>
</div>

<script src="${resources}/js/jquery-2.2.2.min.js"></script>
<script src="${resources}/js/common.js"></script>
 
<script>

      var scoretotlaVal = parseFloat('${allCartPrice*merchantRate.score}');//全选统计的积分
       var selectScore  = parseFloat('${allPrice*merchantRate.score}');//操作时的积分值状态
       checkboxIfSelectAll();
    //加、
	   $(".plus").on("click", function () {
        var minus = $(this).siblings(".minus");
        var goodsId = $(this).siblings(".goodsId").val();
        var merchantId = $(this).siblings("#merchantId_num").val();//商户ID
        var score = $(this).siblings("#score").val();//商品单价积分
        var input = $(this).siblings("#amount");
         
        if ($(this).hasClass("enable")) {
            input.val(Number(input.val()) + 1);
            minus.addClass("enable");
            plusRoMinus(goodsId,input.val());
            $("#goodsNum_"+goodsId).html("x"+input.val());
       
         if($(".checkboxSel_"+goodsId).prop('checked')){      
		       var amount = input.val();//数量 
		       var xiaojiScore = $("#xiaojiScore_"+merchantId).html();  
		       var totalScore = parseFloat($("#total-price").html());
		       //alert(totalScore.toFixed(1))
	          $("#xiaojiScore_"+merchantId).html((parseFloat(xiaojiScore)+parseFloat(score)).toFixed(1)); //小计
	          $("#total-price").html((totalScore+parseFloat(score)).toFixed(1));//总计积分
	           selectScore = (totalScore+parseFloat(score)).toFixed(1);
          }
        }
    });
    
    //减
    $(".minus").on("click", function () {
       
        var input = $(this).siblings("#amount");
        var goodsId = $(this).siblings(".goodsId").val();
        var merchantId = $(this).siblings("#merchantId_num").val();//商户ID
        var score = $(this).siblings("#score").val();//商品单价积分
       
        if(input.val()>1){
             
             input.val(parseInt(input.val())-1);
             plusRoMinus(goodsId,input.val()); 
             $("#goodsNum_"+goodsId).html("x"+input.val());
              
         if($(".checkboxSel_"+goodsId).prop('checked')){          
           var amount = input.val();//数量 
	       var xiaojiScore = $("#xiaojiScore_"+merchantId).html();  
	       var totalScore = parseFloat($("#total-price").html());
	          
          $("#xiaojiScore_"+merchantId).html((parseFloat(xiaojiScore)-parseFloat(score)).toFixed(1)); //小计
          $("#total-price").html((totalScore-parseFloat(score)).toFixed(1));//总计积分
          selectScore = parseFloat(selectScore)-parseFloat(score).toFixed(1);
         }
		}else{
         
           var goodsId = $(this).siblings(".goodsId").val();
			common.alert({
				dialog : true,
				content : '确认删除该商品吗',
				ok : function() { 
					
					var url = "${path}/manage/commodityCart/delCartItem"; 
			       $.post(url,{"goodsId":goodsId,"marketId":4},function(resultjson){
			             if ('000' == resultjson.code) { 
								 window.location.reload();
							}
			       }); 
					
				},
				cancelValue : '取消',
				cancel : function() {
				}
			})

			return;
		}
          
        
    });
    
    //选择商品
      $(".checkboxSel").on("click", function () {
       
        var obj = $(this);
        
        var goodsIds = obj.val();
  
         
        if(obj.prop('checked')){ 
         
        var merchantId = obj.siblings("#merchantId").val();//商户ID
        var goodsId = obj.siblings("#goodsId").val();//商品
        var amount = $(".amount_"+goodsId).val();//数量 
     
        selectCartItem(goodsIds,2,amount); 
        var score = obj.siblings("#score").val();//单价积分
          update(merchantId,goodsId,score,2);
          
        }else{
            
	      var merchantId = obj.siblings("#merchantId").val();//商户ID
	      var goodsId = obj.siblings("#goodsId").val();//商品
	      var amount = $(".amount_"+goodsId).val();//数量 
	       selectCartItem(goodsIds,1,amount); 
	      var score = obj.siblings("#score").val();//单价积分
          update(merchantId,goodsId,score,1);
          
        }  
         
       var total = $("input[name='checkboxname']:checkbox").length;//checkbox数量
       var checked = $("input[name='checkboxname']:checked").length;//选中的数量
       
       if(total==checked){
         $("#selectAll").prop('checked',true);
       }else{
         $("#selectAll").prop('checked',false);
       }
        
        
    });
    
    //更新金额   商户ID,商品,单价积分
    function update(merchantId,goodsId,score,status){
    
       var amount = $(".amount_"+goodsId).val();//数量 
       var xiaojiScore = parseFloat($("#xiaojiScore_"+merchantId).html());  
       var totalScore = parseFloat($("#total-price").html());
       
       if(status==2){
          $("#xiaojiScore_"+merchantId).html((xiaojiScore+parseFloat(score*amount)).toFixed(1)); //小计
          $("#total-price").html((totalScore+(score*amount)).toFixed(1));//总计积分
            
       }
       
       if(status==1){
         $("#xiaojiScore_"+merchantId).html((xiaojiScore-parseFloat(score*amount).toFixed(1)).toFixed(1)); //小计
          $("#total-price").html((totalScore-(score*amount).toFixed(1)).toFixed(1));//总计积分
          
       }
    
    }
      
    //全选
     $("#selectAll").on("click", function(){
    
      var obj = $(this);
      
      $("input[type='checkbox']").prop('checked', $(obj).prop('checked'));
      
      if(obj.prop('checked')){ 
              selectAll(2); 
            $("#total-price").html((selectScore+(scoretotlaVal-selectScore)).toFixed(1));//总计积分
        }else{ 
              selectAll(1); 
              //scoretotlaVal = 0;
            $("#total-price").html(0);//总计积分
        } 
    
    });
		   
    //选中购物车的商品
    function selectCartItem(goodsId,status,amount){
    
       var url = "${path}/manage/commodityCart/checkCartItem";
    
       $.get(url,{"goodsId":goodsId,"status":status,"marketId":4,"amount":amount},function(data){
       });
    }
    
    
    //全选
    
    function selectAll(status){
     
       var url = "${path}/manage/commodityCart/checkCart";
     //marketId 4 积分商城 
	 $.get(url,{"status":status,"marketId":4},function(data){
       }); 
    }
     
      
     
     //是否全选
     function checkboxIfSelectAll(){
 
				     var ifSelectAll = true;
				     $("input[name='checkboxname']:checkbox").each(function (){ 
					 
					   if($(this).attr("checked")){
					            //  alert(this.value); //value是checkbox的值
					    }else{
					      ifSelectAll = false;
					    } 
					    
					   });
					   
			     if(ifSelectAll){
			      $("#selectAll").prop('checked',true);
			    }else{
			      $("#selectAll").prop('checked',false);
			    } 
	   }
	    
	    //加或减  商品ID  数量
	    function plusRoMinus(goodsId,amount){
	       
	       var url = "${path}/manage/commodityCart/update";
		     //marketId 4 积分商城 
			 $.get(url,{"goodsId":goodsId,"amount":amount,"marketId":4},function(data){
		     }); 
	    
	    }
	    
	    //提交
	    $(".submit-order").click(function(){
	    
	         var checked=$("input[name='checkboxname']:checked");
	         if(checked.length>0){
	            window.location.href="${path}/manage/score/cart/ordersubmit";
	         }else{
	           common.alert({content:"至少选择一个"});
	         }
	    
	    });
 
    
</script>
</body>
</html>